package com.example.run_game;

import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringReader;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMapOptions;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;
import com.google.android.gms.maps.model.PolylineOptionsCreator;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.location.Location;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class PostRunInformation extends FragmentActivity
{
	protected MapFragment mapFragment;
    protected Fragment fragment;
    protected MapView mapView;

	SectionsPagerAdapter mSectionsPagerAdapter;

	ViewPager mViewPager;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_post_run_information);
		
		

		// Parse the XML route
		Bundle b = getIntent().getExtras();
		String path = "";
		int exp = 0;
		int calories = 0;
		if (b!=null)
		{
			path 	= b.getString("path");
			exp 	= b.getInt("xp");
			calories= b.getInt("calories");
		}
		ArrayList<Location> route = null;
		try
		{
			try
			{
				route = XMLParser.parseXMLRoute(openFileInput(path));
			} catch (SAXException e)
			{
				e.printStackTrace();
			}
		} catch (FileNotFoundException e)
		{
				e.printStackTrace();
		}
		// Create the adapter that will return a fragment for each of the three
		// primary sections of the app.
		mSectionsPagerAdapter = new SectionsPagerAdapter(
				getSupportFragmentManager(), route, exp, path, calories);
		
		// Set up the ViewPager with the sections adapter.
		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(mSectionsPagerAdapter);

	}

	
	
    public MapView getMapView() {
        if (mapView == null) {
            mapView = new MapView(this);
        }

        return mapView;
    }

	/**
	 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
	 * one of the sections/tabs/pages.
	 */
	public class SectionsPagerAdapter extends FragmentPagerAdapter
	{
		ArrayList<Location> mMarkers;
		int xp;
		int calories;
		String routename;
		
		public SectionsPagerAdapter(FragmentManager fm, ArrayList<Location> route, int experience, String rn, int cals)
		{
			super(fm);
			mMarkers = route;
			xp = experience;
			routename = rn;
			calories = cals;
		}

		@Override
		public Fragment getItem(int position)
		{
			switch (position)
			{
				case 0:
					Fragment f = RunInfoFragment.newInstance(mMarkers, xp, routename, calories);
					return f;
				case 1:
					return CustomMapFragment.create(null, mMarkers, routename.replaceAll("route", "checkpoint"));
				case 2:
					return GraphDisplayFragment.newInstance(mMarkers);
				default:
					return null;
			}
		}
		
		SupportMapFragment gMapFragment()
		{
		    int result = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
		    System.out.println("isGooglePlayServicesAvailable = "+result);
		    System.out.println("success code = "+ConnectionResult.SUCCESS);
			try
			{
				MapsInitializer.initialize(getBaseContext());
			} catch (GooglePlayServicesNotAvailableException e)
			{
				e.printStackTrace();
			}
			SupportMapFragment map = SupportMapFragment.newInstance();
			GoogleMap gm = map.getMap();
			int count=0;
			while (gm == null){
				gm = map.getMap();
			 	System.out.println(gm);
			 	if (count>1000)
			 		break;
			 	else
			 		count++;
			}
			System.out.println(gm);
			return map;
		}

		@Override
		public int getCount()
		{
			// Show 2 total pages.
			return 3;
		}

		@Override
		public CharSequence getPageTitle(int position)
		{
			switch (position) {
			case 0:
				return getString(R.string.title_section1);
			case 1:
				return getString(R.string.title_section2);
			case 2:
				return getString(R.string.title_section3);
			}
			return null;
		}
	}
}
